[#include "../../../common/html.ftl" /]
[@html]
    [@head title="用户列表" css=["fw", "bootstrap-table", "ztree"] /]
    [@body]
        [@main ]
            [@deptTreeLeft /]
            <div class="fw-tables">
                [#--表格主体内容--操作栏--]
                [@mainHeader]
                [#--[@shiro.hasPermission name="system:user:add"]--]
                    <button class="am-btn am-btn-success am-radius" onclick="$.operate.addFull()">添加</button>
                [#--[/@shiro.hasPermission]--]
                [/@mainHeader]

                [#--表格主体内容--搜索栏--]
                [@searchForm searchId="form-user-search" ]
                    <input id="deptId" name="deptId" type="hidden" />
                    <div class="am-form-group">
                        <input type="text" name="loginName" class="am-form-field am-input-sm am-radius"
                               placeholder="登录名称">
                    </div>
                    <div class="am-form-group">
                        <input type="text" name="userName" class="am-form-field am-input-sm am-radius"
                               placeholder="用户姓名">
                    </div>
                    <div class="am-form-group">
                        <input type="number" name="phonenumber" class="am-form-field am-input-sm am-radius"
                               placeholder="手机号码">
                    </div>
                    <div class="am-form-group">
                        <select name="userType" class="am-radius" data-am-selected placeholder="用户类型">
                            <option value=""></option>
                            [#list dict.getType("sys_user_type") as dict]
                                <option value="${dict.dictValue}">${dict.dictLabel}</option>
                            [/#list]
                        </select>
                    </div>
                    <div class="am-form-group">
                        <select name="status" class="am-radius" data-am-selected placeholder="用户状态">
                            <option value=""></option>
                            [#list dict.getType("sys_status") as dict]
                                <option value="${dict.dictValue}">${dict.dictLabel}</option>
                            [/#list]
                        </select>
                    </div>
                [/@searchForm]

                <div class="fw-table">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        [/@main]

        [#--PC/APP 右侧悬浮按钮--]
        [@rightToolbar /]

        [@script js=["fw", "bootstrap-table", "ztree"] ]
            <script type="text/javascript" src="${ctx}/osfw/system/dept/dept-select.js"></script>
            <script type="text/javascript">
                [#--var editFlag = "${hasPermission('system:user:edit')}";--]
                [#--var removeFlag = "${hasPermission('system:user:remove')}";--]
                [#--var resetPwdFlag = "${hasPermission('system:user:resetPwd')}";--]
                var editFlag = "";
                var removeFlag = "";
                var resetPwdFlag = "";
                var isAdministrator = ${convert.toJson(common.isAdministrator())};
                var datasYhlx = ${convert.toJson(dict.getType("sys_user_type"))};
                var prefix = ctx + "osfw/system/manage/user";

                $(function () {
                    queryUserList();
                    queryDeptTreeLeftWithCount();
                });

                function queryUserList() {
                    var options = {
                        url: prefix + "/list",
                        createUrl: prefix + "/add",
                        updateUrl: prefix + "/edit/{id}",
                        removeUrl: prefix + "/remove",
                        sortName: "userId",
                        sortOrder: "asc",
                        modalName: "用户",
                        uniqueId: "userId",
                        columns: [
                            {
                                checkbox: true
                            },
                            {
                                title: "序号",
                                formatter: function (value, row, index) {
                                    return $.table.serialNumber(index);
                                }
                            },
                            {
                                field: 'loginName',
                                title: '账号',
                                formatter: function (value, row, index) {
                                    return $.table.tooltip(value, 20);
                                }
                            },
                            {
                                field: 'userName',
                                title: '用户姓名',
                                formatter: function (value, row, index) {
                                    return $.table.tooltip(value, 5)
                                }
                            },
                            {
                                field: 'dept.deptName',
                                title: '所属部门',
                                formatter: function (value, row, index) {
                                    return $.table.tooltip(value, 7)
                                }
                            },
                            {
                                field: 'userPost',
                                title: '岗位名称',
                                visible: false,
                                formatter: function (value, item, index) {
                                    return $.table.tooltip(value, 10)
                                }
                            },
                            {
                                field: 'email',
                                title: '邮箱',
                                visible: false
                            },
                            {
                                field: 'phonenumber',
                                title: '手机号码',
                            },
                            {
                                field: 'userType',
                                title: '用户类型',
                                visible: false,
                                formatter: function (value, row, index) {
                                    return $.table.selectDictLabel(datasYhlx, value);
                                }
                            },
                            {
                                visible: editFlag == 'hidden' ? false : true,
                                title: '用户状态',
                                formatter: function (value, row, index) {
                                    return statusTools(row);
                                }
                            },
                            {
                                title: '操作',
                                cellStyle: operCellStyle,
                                formatter: function (value, row, index) {
                                    var actions = [];
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editFull(\'' + row.userId + '\')">编辑</a> ');
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + removeFlag + '"  href="javascript:void(0)" onclick="$.operate.remove(\'' + row.userId + '\')">删除</a> ');
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + resetPwdFlag + '"  href="javascript:void(0)" onclick="resetPwd(\'' + row.userId + '\')">重置密码</a> ');
                                    // actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '"  href="javascript:void(0)" onclick="authRole(\'' + row.userId + '\')">分配角色</a>');
                                    return actions.join('');
                                }
                            }]
                    };
                    $.table.init(options);
                }

                /* 用户管理-重置密码 */
                function resetPwd(userId) {
                    var initPassword = ${convert.toJson(config.getKey('sys.user.initPassword'))};
                    $.modal.confirm("确认要重置密码吗？<br/>" + "重置后密码为：" + initPassword, function () {
                        $.operate.post(prefix + "/resetPwd", {"userId": userId, "password": initPassword});
                    });
                }

                /* 用户管理-分配角色 */
                function authRole(userId) {
                    var url = prefix + '/authRole/' + userId;
                    $.modal.openFull("用户分配角色", url);
                }

                /* 用户状态显示 */
                function statusTools(row) {
                    if (row.status == 1) {
                        return '<i class=\"am-icon-toggle-off am-text-danger am-text-lg\" onclick="enable(\'' + row.userId + '\')"></i> ';
                    } else {
                        return '<i class=\"am-icon-toggle-on am-text-success am-text-lg\" onclick="disable(\'' + row.userId + '\')"></i> ';
                    }
                }

                /* 用户管理-停用 */
                function disable(userId) {
                    $.modal.confirm("确认要停用用户吗？", function () {
                        $.operate.post(prefix + "/changeStatus", {"userId": userId, "status": 1});
                    });
                }

                /* 用户管理启用 */
                function enable(userId) {
                    $.modal.confirm("确认要启用用户吗？", function () {
                        $.operate.post(prefix + "/changeStatus", {"userId": userId, "status": 0});
                    });
                }

            </script>
        [/@script]
    [/@body]
[/@html]
